package com.py.warehouse.controller;

import com.py.api.PyCodeApi;
import com.py.warehouse.domain.BsWarehouse;
import com.py.warehouse.domain.BsWhLocation;
import com.py.warehouse.domain.WhInventory;
import com.py.warehouse.service.*;
import com.ruoyi.common.core.context.SecurityContextHolder;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.api.domain.SysUser;
import org.apache.tomcat.util.net.openssl.ciphers.Authentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 仓库列表Controller
 * 
 * @author ruoyi
 * @date 2024-11-25
 */
@RestController
@RequestMapping("/location")
public class BsLocationController extends BaseController
{

    @Autowired
    private IBsWhLocationService bsWhLocationService;

    @Resource
    private PyCodeApi pyCodeApi;

    @Autowired
    private WhInventoryService whInventoryService;

    /**
     * 获取编码
     */
    @PostMapping("/getcode")
    public Map<String, Object> getCode(@RequestParam(value = "code",required = false) Integer code){
        System.out.println("进来了"+code);
        return pyCodeApi.generateCode(code);
    }

    /**
     * 查询库位
     */
    @RequiresPermissions("warehouse:location:listLocation")
    @GetMapping("/listLocation")
    public TableDataInfo list(BsWhLocation bsWhLocation)
    {
        startPage();
        List<BsWhLocation> list = bsWhLocationService.selectBsWhLocation(bsWhLocation);
        return getDataTable(list);
    }


    /**
     * 删除仓库列表
     */
    @RequiresPermissions("warehouse:location:remove")
    @Log(title = "库位列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{locationIds}")
    public AjaxResult remove2(@PathVariable Long[] locationIds)
    {
        System.out.println("删除");
        return toAjax(bsWhLocationService.deleteBsWhLocationByLocationIds(locationIds));
    }

    /**
     * 修改库位列表
     */
    @RequiresPermissions("warehouse:location:edit")
    @Log(title = "库位列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BsWhLocation bsWarehouse)
    {
        return toAjax(bsWhLocationService.updateBsWhLocation(bsWarehouse));
    }

    /**
     * 修改库位仓库编码
     */
    @PostMapping("/addLocationWarehouseCode")
    public AjaxResult addLocationWarehouseCode(@RequestParam Long[] locationIds,
                                               @RequestParam String warehouseCode)
    {
        return toAjax(bsWhLocationService.updateBsWhLocationByWarehouseCodes(locationIds, warehouseCode));
    }

    /**
     * 新增仓库列表
     */
    @RequiresPermissions("warehouse:location:add")
    @Log(title = "库位列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BsWhLocation bsWhLocation)
    {
        pyCodeApi.saveCode(bsWhLocation.getCodeId(),bsWhLocation.getNumber());
        return toAjax(bsWhLocationService.insertBsWhLocation(bsWhLocation));
    }

    /**
     * 获取库位列表详细信息
     */
    @RequiresPermissions("warehouse:location:query")
    @GetMapping(value = "/{locationId}")
    public AjaxResult getInfo(@PathVariable("locationId") Long locationId)
    {
        return success(bsWhLocationService.selectBsWhLocationByLocationId(locationId));
    }

    /**
     * 根据仓库编号获取库位数量
     */
    @PostMapping("/findBsWhLocationByCount")
    public Integer findBsWhLocationByCount(@RequestParam String warehouseCode)
    {
        return bsWhLocationService.selectBsWhLocationByCount(warehouseCode);
    }

    /**
     * 停用库位
     */
    @PostMapping("/deactivate")
    public Integer updateDeactivate(@RequestParam(value = "locationCode") String locationCode,
                                    @RequestParam(value = "status") String status){
        return bsWhLocationService.updateDeactivate(locationCode,status);
    }

    /**
     * 查找库位里面的商品数量
     */
    @PostMapping("/getLocationCode")
    public Integer selectWhInventoryByWarehouseCode(@RequestParam(value = "locationCode") String locationCode){
        return whInventoryService.selectWhInventoryByWarehouseCode(locationCode);
    }

    /**
     * 根据仓库编号查找库位
     */
    @GetMapping("/selectLocation")
    public List<BsWhLocation> selectLocation(@RequestParam(value = "warehouseCode") String warehouseCode){
        return bsWhLocationService.selectLocationByWarehouseCode(warehouseCode);
    }

    /**
     * 查找库存数量
     */
    @GetMapping("/getInventory")
    public Integer selectWhInventoryByWarehouseCode(WhInventory whInventory){
        return whInventoryService.selectInventory(whInventory);
    }

    @GetMapping("/updateWarehouseCode")
    public Integer updateWarehouseCode(@RequestParam("warehouseCode") String warehouseCode){
        return bsWhLocationService.updateRemoveWarehouseCode(warehouseCode);
    }
}
